﻿using System;
using System.ComponentModel;
using System.Reactive.Linq;
using Microsoft.Phone.UserData;
using Rxx.Labs.Properties;

namespace Rxx.Labs.Reactive.Phone
{
	[DisplayName("User Data (New)")]
	[Description("Retrieving contacts and appointments.")]
	public sealed class UserDataLab : BaseConsoleLab
	{
		protected override void Main()
		{
			TraceLine(Instructions.PressAnyKeyToCancel);

			RunExperiments();
		}

		[Description("Searches for the specified contact by name.")]
		public void ContactsExperiment()
		{
			TraceLine(Instructions.PhoneEmulatorContactsSuggestion);

			var name = UserInput("Contact Name> ");

			var search = ObservableUserData
				.SearchContacts(name, FilterKind.DisplayName)
				.SelectMany(e => e.Results);

			using (search.Subscribe(ConsoleOutput))
			{
				WaitForKey();
			}
		}

		[Description("Searches for appointments from now until tomorrow.")]
		public void AppointmentsExperiment()
		{
			TraceLine(Instructions.PhoneEmulatorAppointmentsSuggestion);

			var search = ObservableUserData
				.SearchAppointments(DateTime.Now, DateTime.Today.AddDays(1))
				.SelectMany(e => e.Results)
				.Select(appointment => appointment.Subject);

			using (search.Subscribe(ConsoleOutput))
			{
				WaitForKey();
			}
		}
	}
}